<!--#include file="./header.inc"-->
<%

select case aq
	case "thumb"    	: 	call thumb
	case "watermark" 	: 	call watermark
	case "delete" 		: 	call delete
	case else 			: 	call index
end select

call db.close()

function index()
    %><!--#include file="./view/file.index.html"--><%
end function

function delete()
    dim fns, fn
    fns = request.form("fn").count
    if fns = 0 then app.json "没有文件！", 1
    for i = 1 to fns
        fn = trim(request("fn")(i))
        app.deleteFile fn
    next
    app.json "删除文件成功", 0
end function

function watermark()
	dim fns, fn, jpeg, j , newfn
	dim width, height
	if app.checkObject("persits.jpeg") <> true then app.json "没有安装水印组件！", 1
	fns = request.form("fn").count
	if fns = 0 then app.json "没有图片！", 1
	width = app.int(request.form("width"))
	height = app.int(request.form("height"))
	if width < 1 then width = 200
	if height < 1 then width = 150
	j = 0
	set jpeg = server.createObject("persits.jpeg")
	for i = 1 to fns
		fn = trim(request.form("fn")(i))
		if app.isFile(fn) then
			jpeg.open server.mapPath(fn)
			if jpeg.originalWidth > width then
				jpeg.width = width
				jpeg.height = (jpeg.originalHeight / jpeg.originalWidth) * width
			elseif jpeg.originalWidth > height then
				jpeg.width = (jpeg.originalWidth / jpeg.originalHeight) * height
				jpeg.height = height
			else
				jpeg.width = jpeg.originalWidth
				jpeg.height = jpeg.originalHeight
			end if
			'newfn = replace(fn, right(fn, 4), ".jpg" & right(fn, 4))
			jpeg.save server.mapPath(fn & ".jpg")
			j = j + 1
		end if
	next
	jpeg.close
	set jpeg = nothing
	if j > 0 then
		app.json "生成缩放图 " & j & " 张成功！", 0
	else
		app.json "生成缩放图失败！", 1
	end if
end function

function thumb()
	dim fns, fn, jpeg, j , newfn
	dim thumbWidth, thumbHeight
	if app.checkObject("persits.jpeg") <> true then app.json "没有安装水印组件！", 1
	fns = request.form("fn").count
	if fns = 0 then app.json "没有图片！", 1
	thumbWidth = app.int(app.getConfig("thumbWidth"))
	thumbHeight = app.int(app.getConfig("thumbHeight"))
	if thumbWidth < 1 then thumbWidth = 200
	if thumbHeight < 1 then thumbHeight = 150
	j = 0
	set jpeg = server.createObject("persits.jpeg")
	for i = 1 to fns
		fn = trim(request.form("fn")(i))
		if app.isFile(fn) then
			jpeg.open server.mapPath(fn)
			if jpeg.originalWidth > thumbWidth then
				jpeg.width = thumbWidth
				jpeg.height = (jpeg.originalHeight / jpeg.originalWidth) * thumbWidth
			elseif jpeg.originalWidth > thumbHeight then
				jpeg.width = (jpeg.originalWidth / jpeg.originalHeight) * thumbHeight
				jpeg.height = thumbHeight
			else
				jpeg.width = jpeg.originalWidth
				jpeg.height = jpeg.originalHeight
			end if
			'newfn = replace(fn, right(fn, 4), ".jpg" & right(fn, 4))
			jpeg.save server.mapPath(fn & ".jpg")
			j = j + 1
		end if
	next
	jpeg.close
	set jpeg = nothing
	if j > 0 then
		app.json "生成缩放图 " & j & " 张成功！", 0
	else
		app.json "生成缩放图失败！", 1
	end if
end function
%>